package xin.vote.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import xin.vote.model.Matchs;
import xin.vote.model.Users;
import xin.vote.service.IMatchsService;
import xin.vote.util.Result;

@Controller
@RequestMapping("/admin")
public class MatchsController {
	@Resource
	private IMatchsService matchsService;

	//查询所有比赛
	@RequestMapping("/getAllMatches")
	@ResponseBody
	public Result findAllMatches() {
		List<Matchs> matchsList = matchsService.selectAllMatches();
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setCount(matchsList.size());
		result.setData(matchsList);
		return result;
	}
	
	//顶部动态查询
	@RequestMapping("/findmatchdynamic")
	@ResponseBody
	public Result findMatchDynamic(@RequestParam(value = "matchname", required = false) String matchname) {
		Matchs m = new Matchs();
		m.setMatchname(matchname);
		
		List<Matchs> MatchsList = matchsService.selectMatchesDynamic(m);
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setCount(MatchsList.size());
		result.setData(MatchsList);
		return result;
	}
	
	//添加比赛
	@RequestMapping("/adminaddmatch")
	@ResponseBody                  // required = false :允许入参为空
	public String addMatches(@RequestParam(value = "matchname", required = false) String matchname,
							 @RequestParam(value = "matchaddrees", required = false) String matchaddrees,
							 @RequestParam(value = "matchtime") String matchtime,
							 @RequestParam(value = "matchinfo") String matchinfo) {
		Matchs m = new Matchs();
		m.setMatchname(matchname);
		m.setMatchaddress(matchaddrees);
		m.setMatchtime(matchtime);
		m.setMatchinfo(matchinfo);
		
		int ret = matchsService.insertMatches(m);
		if(ret > 0) {
			return "1";
		}else {
			return "0";
		}
	}
	
	//删除比赛
	@RequestMapping("/admindeletematch")
	@ResponseBody
	public String deleteMatches(@RequestParam(value = "matchid", required = false) Integer matchid) {
		int ret = matchsService.deleteMatches(matchid);
		if(ret > 0) {
			return "1";
		}else {
			return "0";
		}
	}
}
